import zh from "share/i18n/zh_CN";
import en from "share/i18n/en_US";
import ja from "share/i18n/ja_JP";
import zh_TW from "share/i18n/zh_TW";
import React from 'react'
import config from "config";

/**
 * Created by zaranengap on 2017/7/3.
 *
 * Clear by zaranengap on 2019/3/5
 * 这个文件目前放这两个方法就够了，主要是给redux写工具用的
 * 之后如果有redux相关的 middleWare、store 工具的方法可以放在这里
 * 一般的公用放到 ./common.js 里去，或者新建一个文件
 *
 * -------以上删除-------
 *
 * 因为最下方那个方法的原因，这个文件存储store初始化之前就可以进行操作的方法
 * common里放store操作后可以使用的方法
 * 竟然要分reducer里是否使用来分文件方法，fk
 *
 */

// reducer生成器，为了以后使用方便，起名为create reducer的简写
export function cr (initialState, handlers) {
  return function reducer(state = initialState, action) {
    if (handlers.hasOwnProperty(action.type)) {
      return handlers[action.type](state, action);
    } else {
      return state;
    }
  }
}

// action生成器的生成器，同样原因，起名为create action creator的简写
export function cac (type, ...argNames){
  return function(...args) {
    let action = { type };
    argNames.forEach((arg, index) => {
      action[argNames[index]] = args[index]
    });
    return action
  }
}

/**
 * 这个方法在reducer里用到了，reducer是在store初始化之前调用的
 * 所以如果把这个文件移到common.js里messages就会报错，报store is undefined
 * 因为common.js引用了constants.js, 报错主要就是constants里的，那个时候store.js还没有调用
 * 所以这个方法移动不了，也不想新建文件存了
 */
export function getLanguageObjByCode(langCode, navigator) {
  //后端返回语言列表样例
  // [
  //   {"code":"zh_CN","value":"简体中文","comments":"简体中文","type":"language"},
  //   {"code":"en","value":"英文","comments":"English","type":"language"},
  //   {"code":"ja","value":"日语","comments":"日本语","type":"language"},
  //   {"code":"zh_TW","value":"繁体中文","comments":"繁体中文","type":"language"}
  // ]

  //locale设置ant-d语言的  locale全部小写
  //麦当劳默认给中文
  return {
    locale: 'zh',
    messages: zh,
    code: 'zh_cn'
  }
  //默认给英文
  let language = {
    locale: 'en',
    messages: en,
    code: 'en'
  };
  if(navigator){
    //如果没有登录，从浏览器获取语言环境
    switch(navigator.language){
      case 'en':
        language = {
          locale: 'en',
          messages: en,
          code: 'en'
        };
        break;
      case 'zh-CN'://浏览器是zh-CN，接口是zh_CN
        language = {
          locale: 'zh',
          messages: zh,
          code: 'zh_cn'
        };
        break;
      case 'ja':
        language = {
          locale: 'ja_jp',
          messages: ja,
          code: 'ja'
        };
        break;
      case 'zh-TW':
        language = {
          locale: 'zh_TW',
          messages: zh_TW,
          code: 'zh_tw'
        };
        break;
    }
  }else {
    switch(langCode.toLowerCase()){
      case 'en':
        language = {
          locale: 'en',//ant-d
          messages: en, //引用哪个多语言json
          code: 'en'//传给后台的
        };
        break;
      case 'zh_cn':
        language = {
          locale: 'zh',
          messages: zh,
          code: 'zh_cn'
        };
        break;
      case 'ja':
        language = {
          locale: 'ja_jp',
          messages: ja,
          code: 'ja'
        };
        break;
      case 'zh_tw':
        language = {
          locale: 'zh_TW',
          messages: zh_TW,
          code: 'zh_tw'
        };
        break;
    }
  }
  return language;
}


export function asyncComponent (componentFunc){
  class Wrapper extends React.Component{
    constructor (props){
      super(props)
      this.state={
        component: null
      }
      this.init();
    }
    init(){
      if(Wrapper.asyncComponentFunc){
        Wrapper.asyncComponentFunc((component)=>{
          this.setState({
            component
          })
        })
      }
    }
    render(){
      let {component}= this.state
      if(!component){
        return <div></div>
      }
      return React.createElement(component, {...this.props}, this.props.children);
    }
  }
  Wrapper.asyncComponentFunc= componentFunc
  return Wrapper
}


/*
* 处理socket登录
* */
export const getWsUrl = (url) => {
  console.log('getWsUrl')
  let wsUrl= typeof config.wsUrl === 'function'? config.wsUrl() : config.wsUrl
  let corpIdProxyInfo= window.corpIdProxyInfo
  if (corpIdProxyInfo && corpIdProxyInfo.isNeedProxy) {
    let domainHost= sessionStorage.getItem('domainHost') || 'https://api-dd.huilianyi.com';
    let host= domainHost.split(':')[1]
    let protocal= location.protocol.replace('http', 'ws')
    wsUrl= `${protocal}${host}`;
    url= `${wsUrl}${url.startsWith('/') ? url : '/' + url}&corpid=${corpIdProxyInfo.corpId}&x-helios-client=react web`;
  }else{
    url= `${wsUrl}${url.startsWith('/') ? url : '/' + url}`
  }
  return url
};

/*
* 处理多云登录时的proxy
* */
export const getProxy = ()=>{
  let proxy= ''
  // 使用token来判断用户是否已经登录，未登录的则加proxy
  let token= sessionStorage.getItem('hly.token') || localStorage.getItem('hly.token')
  if(location.pathname === '/' && !token){
    proxy= '/proxy'
  }
  return proxy
}
